﻿using System;
using System.Collections.Generic;
using System.Data;
using System.IO;
using System.Security.Cryptography;
using System.Text;
using System.Web.Security;
using System.Windows.Forms;

namespace QT
{
    /// <summary>
    /// 系统工具操作类
    /// </summary>
    public class Tools
    {
        #region Alert

        /// <summary>
        /// 弹出对话框，无参数
        /// </summary>
        public static void Alert()
        {
            MessageBox.Show("欢迎使用大秦时代管理系统");
        }

        /// <summary>
        /// 弹出对话框,带参数
        /// </summary>
        /// <param name="mystr">对话框文字</param>
        public static void Alert(string mystr)
        {
            MessageBox.Show(mystr);
        }

        #endregion

        #region MD5

        /// <summary>
        /// MD5加密，返回加密后的字符串
        /// </summary>
        /// <param name="str">需要加密的明文</param>
        /// <returns>加密后的密文</returns>
        public static string MD5(string str)
        {
            return FormsAuthentication.HashPasswordForStoringInConfigFile(str, "MD5");
        }

        #endregion

        #region SHA1

        /// <summary>
        /// SHA1加密，返回加密后的字符串
        /// </summary>
        /// <param name="str">需要加密的明文</param>
        /// <returns>加密后的密文</returns>
        public static string SHA1(string str)
        {
            return FormsAuthentication.HashPasswordForStoringInConfigFile(str, "SHA1");
        }

        #endregion

        #region 3DES

        private static string sKey = "5tpMC2dovLDHzt/pRr/SOGSJPXANLG51"; //私钥

        /// <summary>
        /// 加密
        /// </summary>
        /// <param name="encryptValue">待加密的字符串</param>
        /// <returns>加密成功返回加密后的字符串，失败返回源字符串</returns>
        public static string EncyptString(string encryptValue)
        {
            try
            {
                TripleDES des = new TripleDESCryptoServiceProvider();
                des.Key = Convert.FromBase64String(sKey);
                des.Mode = CipherMode.ECB;

                byte[] valBytes = Encoding.UTF8.GetBytes(encryptValue);
                ICryptoTransform transform = des.CreateEncryptor();

                MemoryStream ms = new MemoryStream();
                CryptoStream cs = new CryptoStream(ms, transform, CryptoStreamMode.Write);
                cs.Write(valBytes, 0, valBytes.Length);
                cs.FlushFinalBlock();
                byte[] returnBytes = ms.ToArray();
                cs.Close();

                return Convert.ToBase64String(returnBytes);
            }
            catch
            {
                return encryptValue;
            }
        }

        /// <summary>
        /// 加密,带密钥
        /// </summary>
        /// <param name="encryptValue">待加密的字符串</param>
        /// <param name="encryptKey">加密密钥</param>
        /// <returns>加密成功返回加密后的字符串，失败返回源字符串</returns>
        public static string EncyptString(string encryptValue, string encryptKey)
        {
            try
            {
                TripleDES des = new TripleDESCryptoServiceProvider();
                des.Key = Convert.FromBase64String(encryptKey);
                des.Mode = CipherMode.ECB;

                byte[] valBytes = Encoding.UTF8.GetBytes(encryptValue);
                ICryptoTransform transform = des.CreateEncryptor();

                MemoryStream ms = new MemoryStream();
                CryptoStream cs = new CryptoStream(ms, transform, CryptoStreamMode.Write);
                cs.Write(valBytes, 0, valBytes.Length);
                cs.FlushFinalBlock();
                byte[] returnBytes = ms.ToArray();
                cs.Close();

                return Convert.ToBase64String(returnBytes);
            }
            catch
            {
                return encryptValue;
            }
        }

        /// <summary>
        /// 解密
        /// </summary>
        /// <param name="decryptedValue">待解密的字符串</param>
        /// <returns>解密成功返回解密后的字符串，失败返回源字符串</returns>
        public static string DecryptString(string decryptedValue)
        {
            try
            {
                TripleDESCryptoServiceProvider des = new TripleDESCryptoServiceProvider();
                des.Key = Convert.FromBase64String(sKey);
                des.Mode = CipherMode.ECB;

                byte[] valBytes = Convert.FromBase64String(decryptedValue);
                ICryptoTransform transform = des.CreateDecryptor();

                MemoryStream ms = new MemoryStream();
                CryptoStream cs = new CryptoStream(ms, transform, CryptoStreamMode.Write);
                cs.Write(valBytes, 0, valBytes.Length);
                cs.FlushFinalBlock();
                byte[] returnBytes = ms.ToArray();
                cs.Close();

                return Encoding.UTF8.GetString(returnBytes);
            }
            catch
            {
                return decryptedValue;
            }
        }

        /// <summary>
        /// 解密，带密钥
        /// </summary>
        /// <param name="decryptedValue">待解密的字符串</param>
        /// <param name="decryptedKey">解密密钥</param>
        /// <returns>解密成功返回解密后的字符串，失败返回源字符串</returns>
        public static string DecryptString(string decryptedValue, string decryptedKey)
        {
            try
            {
                TripleDESCryptoServiceProvider des = new TripleDESCryptoServiceProvider();
                des.Key = Convert.FromBase64String(decryptedKey);
                des.Mode = CipherMode.ECB;

                byte[] valBytes = Convert.FromBase64String(decryptedValue);
                ICryptoTransform transform = des.CreateDecryptor();

                MemoryStream ms = new MemoryStream();
                CryptoStream cs = new CryptoStream(ms, transform, CryptoStreamMode.Write);
                cs.Write(valBytes, 0, valBytes.Length);
                cs.FlushFinalBlock();
                byte[] returnBytes = ms.ToArray();
                cs.Close();

                return Encoding.UTF8.GetString(returnBytes);
            }
            catch
            {
                return decryptedValue;
            }
        }

        #endregion
    }
}
